<?php 

/**
* @Copyright Copyright (C) 2012 - JoniJnm.es
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
**/

class modLcaCache {	
	var $params;
	var $on;
	var $expired;
	var $time;
	var $path;
	var $file;
	var $xml;
	var $show;
	
	function __construct(&$params) {
		$this->params =& $params;
		$this->on = $params->get('usecache', 1);
		$this->expired = false;
		$this->time = 60*$params->get('cache_time', 900);
		$this->path = JPATH_BASE.DS.'cache'.DS.'mod_lca'.DS;
	}
	function init() {
		ob_start();
		print_r($this->params);
		$html = ob_get_contents();
		@ob_end_clean();
		$md5 = md5($html);
		$this->file = $this->path.$md5.'.xml';
		if (file_exists($this->file)) {
			$this->xml = simplexml_load_file($this->file);
			if ($this->xml->date < time() || !$this->xml->html) {
				$this->expired = true;
			}
		}
		else
			$this->expired = true;
	}
	function show() {
		$this->show = $this->xml->show;
		echo $this->xml->html;
	}
	function check() {
		return $this->on && !$this->expired;
	}
	function start() {
		if ($this->on && $this->expired)
			ob_start();
	}
	function end() {
		if ($this->on && $this->expired) {
			$html = ob_get_contents();
			@ob_end_clean();
			echo $html;
			if (file_exists($this->file))
				unlink($this->file);
			if (!file_exists($this->path))
				mkdir($this->path);
			file_put_contents($this->file, '<xml><show>'.$this->show.'</show><date>'.(time()+$this->time).'</date><html><![CDATA['.$html.']]></html></xml>');
		}
	}
}